package com.tgy.基础学习.静态代理练习;

import java.util.Hashtable;
import java.util.Map;
/*
静态代理总结：
1，真实对象和代理对象都要实现同一个接口
2.代理对象要代理真实角色
3，好处：代理对象可以做很多真实对象做不到得事情，真实对象专注做自己得事情
 */

/**
 * @author 送你一块二向箔
 * @date 2021/11/06 19:53
 * 静态代理学习
 */
public class StaticProxy {
    public static void main(String[] args) {
        Hashtable<String, Object> hashMap = new Hashtable<String, Object>();
        WeddingComany weddingComany = new WeddingComany(new You());
        weddingComany.HappyMarry("String a");

        //简化
        new WeddingComany(new You()).HappyMarry("String");


        //方法引用的方式
        Marry marry;
        WeddingComany weddingComany1 = new WeddingComany(marry = System.out::println);
        weddingComany1.HappyMarry("结婚了开心");
        new Thread(()->{
            System.out.println("6666");
        }).start();
    }
}

@FunctionalInterface
interface Marry {
    abstract void HappyMarry(String a);
}

//真实角色
class You implements Marry{

    @Override
    public void HappyMarry(String a) {
        System.out.println("开心");
    }
}


//代理角色
class WeddingComany implements Marry {

//代理谁？真实目标角色
private Marry targat;

public WeddingComany(Marry targat) {
    this.targat = targat;
}


@Override
public void HappyMarry(String a) {
    before();
    this.targat.HappyMarry(a);//这就是真实对象在做事情
    after();

}

private void before() {
    System.out.println("结婚前布置现场");
}

private void after() {
    System.out.println("结婚后收尾款");
}
}

